import {
  Scheduler,
  LocaleManager,
  ProjectModel,
  Splitter,
} from 'major-schedulerpro'
import 'major-schedulerpro/schedulerpro.stockholm.css'
import viewPreset, {
  standardTime,
  getDayViewPresetData,
} from './config/viewPreset'

import {
  eventBodyTemplate,
  eventRenderer,
} from './config/schedulerEventRendering'
// 国际化
import locale from './config/locale.Cn'
import featuresCommon from './config/features'

LocaleManager.registerLocale('Cn', { desc: 'zh-CN', locale: locale })
LocaleManager.locale = LocaleManager.locales.Cn
export default class MajorGantt {
  constructor(dom) {
    this.scheduler = {}
    // 事件
    this.eventsData = []
    // 泳道
    this.resourcesData = []
    // 关系
    this.dependencies = [] // 关系
    // 甘特图的标准时间（T0时间）
    this.standardTime = +new Date()
    // 甘特图开始时间
    this.startDate = new Date()
    // 甘特图结束时间
    this.endDate = new Date()
    this.init(dom)
  }
  init(dom) {
    const project = this.getSchedulerProjectModel()
    this.scheduler = new Scheduler({
      useInitialAnimation: false,
      appendTo: dom,
      project: project,
      rowHeight: 45, // 80
      barMargin: 5,
      managedEventSizing: true, // 通过样式控制高度
      // resourceMargin: 5,
      minHeight: '10em',
      hideWhenEmpty: false,
      zoomOnTimeAxisDoubleClick: false,
      zoomOnMouseWheel: false, // ctrl+mousewheel 缩放时间轴
      hideFooters: true,
      enableDeleteKey: false,
      multiEventSelect: true, // 多选任务
      eventSelectionDisabled: false,
      createEventOnDblClick: false,
      presets: getDayViewPresetData(), // 自定义时间轴
      eventColor: '#2196f3',
      /**
       * 如果是里程碑事件 使用svg circle 标记
       * @param {*} eventRecord
       * @returns
       */
      htmlEncode: false,
      viewPreset: 'dayAndMonth',
      cls: 'main-scheduler',

      // 排序保证相同时间的在一起
      horizontalEventSorterFn(a, b) {
        const sortGroup = a.get('sortSign')
        const sortGroup1 = b.get('sortSign')
        console.log('排序了吧', sortGroup, sortGroup1)
        if (sortGroup) {
          return sortGroup - sortGroup1
        }
        const startA = a.startDate,
          endA = a.endDate
        const startB = b.startDate,
          endB = b.endDate

        const sameStart = startA - startB === 0

        if (sameStart) {
          return endA > endB ? -1 : 1
        } else {
          return startA < startB ? -1 : 1
        }
      },
      columns: [
        {
          type: 'tree',
          width: 340,
          height: 300,
          showColumnPicker: false,
          leafIconCls: null,
          indentSize: 3,
          // expandIconCls: 'b-fa b-fa-plus',
          // collapseIconCls: 'b-fa b-fa-minus',
          sortable: false,
          // sum: 'count',
          htmlEncode: false,
          renderer: function (renderData) {
            const { record } = renderData
            // if (['dwrwst'].includes(record.id)) {
            //   renderData.size.height = 30
            // } else {
            //   renderData.size.height = 120
            // }
            // renderData.size.height = ['dwrwst'].includes(record.id) ? 30 : 0
            return record.name
          },

          /*  headerRenderer: () => {
              return '<div  class="calendar-header">任务规划</div>'
            } */
        },
      ],
      // 自定义事件模板
      // eventBodyTemplate: eventBodyTemplate,
      // 自定义事件渲染逻辑
      // eventRenderer: eventRenderer,
      features: featuresCommon(standardTime),
      readOnly: true,
    })
  }
  /**
   * 获取资源负荷图 projectmodel
   * @returns
   */
  getSchedulerProjectModel() {
    return new ProjectModel({
      eventsData: this.eventsData,
      resourcesData: this.resourcesData,
      dependenciesData: this.dependencies,
      startDate: this.startDate,
      endDate: this.endDate,
      // eventModelClass: CustomEventModel,
      listeners: {
        load: () => {
          console.log('加载完毕')
        },
      },
    })
  }
  setGanttOptions(options) {
    console.log(options)
    // 事件
    options.eventsData && (this.eventsData = options.eventsData)
    // 泳道
    options.resourcesData && (this.resourcesData = options.resourcesData)
    // 关系
    this.dependencies = options?.dependencies ?? []

    //  开始时间
    options.startDate && (this.startDate = options.startDate)
    // 结束时间
    options.endDate && (this.endDate = options.endDate)
    // 重新设置开始、结束时间
    if (this.scheduler) {
      this.standardTime = +new Date(options.standardTime)
      // this.standardTime.value = this.standardTime

      this.scheduler.startDate = this.startDate
      this.scheduler.endDate = this.endDate
      // this.scheduler.lanesData = this.lanesData
    }
  }
  /**
   * 重新渲染甘特图
   * @param {*} param0
   */
  async resetGanttProject(options) {
    this.setGanttOptions(options)
    // this.setGanttLoadingState('加载中...')

    // 获取甘特图 project
    const GANTT_PROJET = this.getSchedulerProjectModel()
    this.scheduler.project = GANTT_PROJET
    await this.scheduler.project.commitAsync()
    console.log('自定义时间轴')
    console.log(this.scheduler.presets)

    this.scheduler.timeAxis.trigger('refresh')
    // this.setGanttLoadingState(false)
  }
}
